package book

class BookController {
	def scaffold = true
	def bookService
	
    def index = { redirect(action: randomBookService)}
	
	def randomBookService =
	{
		def now = new Date()
		def randomBook = bookService.getRandomBook()
		[book:	randomBook, fetched: now, gradeLevel: gradeLevel(randomBook)]
	}
	
	def randomBookAjax =
	{
		def now = new Date()
		def randomBook = bookService.getRandomBook()
		render (view:"randomBookAjax", model: [book: randomBook, fetched: now, gradeLevel: gradeLevel(randomBook)])
	}
	
	String gradeLevel(Book book)
	{
		if (book.minGrade == book.maxGrade)
		{
	  		book.minGrade.toString()
		}
		else
		{	
			book.minGrade.toString() + " through " + book.maxGrade.toString()
		}
	}
}
